<?php
class model_permission extends database_mysql
{
    
    public function mdp_get_list_permission($page=1,$limit=1000, $cid = null, $order_by = 'ID')
    {
        $str = null;
        if(!empty($limit))
        {
			$page=($page==0 || empty($page)?1:$page);
			$start = ($page - 1)* $limit; 
			$strlimit = "LIMIT $start, $limit";
		}
		else{
			$strlimit ='';
		}
        $sql_cid = '';	
        if($cid != null && $cid != 'all')
        {
            $sql_cid = " AND ". prefix ."permission.`CID` = '$cid'";
        }
        $sql = "
                SELECT
                ". prefix ."permission.ID,
                ". prefix ."permission.PremissName,
                ". prefix ."permission.CID,
                ". prefix ."permission.Action,
                ". prefix ."permission.`Status`,
                ". prefix ."categories_lang.CatName
                FROM
                ". prefix ."permission
                INNER JOIN ". prefix ."categories_lang ON ". prefix ."permission.CID = ". prefix ."categories_lang.CatID
                WHERE
                prefix_categories_lang.LangID = ".LANG."
                ".$sql_cid."
                ORDER BY ". prefix ."permission.$order_by DESC ".$strlimit;        
        $row = parent::mysql_fetch_all_rows($sql);
        parent::mysql_close_connection();
 		return $row;
    }
    
	public function mdp_get_list_seo_key()
    {
        $sql = "
                SELECT
                ". prefix ."categories_lang.SeoKey,
                ". prefix ."categories.ID
                FROM
                ". prefix ."categories
                INNER JOIN ". prefix ."categories_lang ON ". prefix ."categories.ID = ". prefix ."categories_lang.CatID
                WHERE
                ". prefix ."categories_lang.`Status` = 'show' AND
                ". prefix ."categories.Define <> 'app' AND
                ". prefix ."categories_lang.LangID = 
                ".LANG;
        $row = parent::mysql_fetch_all_rows($sql);
 		return $row;  
    }
    
    public function mdp_get_action_from_seokey($seo_key){
        $sql = " 
                    SELECT
                    ". prefix ."actions.ActName,
                    ". prefix ."actions.`Status`
                    FROM
                    ". prefix ."actions
                    INNER JOIN ". prefix ."com_act ON ". prefix ."actions.ID = ". prefix ."com_act.ActID
                    WHERE
                    ". prefix ."com_act.ComID = $seo_key AND
                    ". prefix ."actions.`Status` = 'true' AND
                    ". prefix ."com_act.`Status` = 'true'

        ";
        $row = parent::mysql_fetch_all_rows($sql);
 		return $row; 
    }
    
    public function mdp_save_permission($table, $data){
        $table = prefix . $table;
        parent::mysql_insert_array($table, $data);
    }
    
    public function mdp_update_permission($table, $data, $condition){
        $table = prefix . $table;
        parent::mysql_update_array($table, $data, $condition);
    }
    
    public function mdp_get_total($table, $cid = null)
    {
        $sql_cid = '';
        if($cid != null && $cid != 'all')
        {
            $sql_cid = " WHERE ". prefix ."permission.CID = $cid";
        }
        $sql = "SELECT COUNT( * ) AS total FROM ". prefix .$table . $sql_cid;
        $result = parent::mysql_select_one($sql);
        return  $result;
    }
    
    public function mdp_get_permission_from_id($id){
        $row = parent::mysql_fetch_one_rows(" 
                    SELECT *
                    FROM
                    ". prefix ."permission
                    WHERE
                    ". prefix ."permission.ID = 
        ".$id);
 		return $row;
    }
    
    public function mdp_get_list_seo_key_from_com($com)
    {
        $sql = " 
                    SELECT
                    ". prefix ."actions.ActName,
                    prefix_actions.ID
                    FROM
                    ". prefix ."categories
                    INNER JOIN ". prefix ."com_act ON ". prefix ."com_act.ComID = ". prefix ."categories.ID
                    INNER JOIN ". prefix ."actions ON ". prefix ."actions.ID = ". prefix ."com_act.ActID
                    WHERE
                    ". prefix ."categories.Com = '$com'";
        $row = parent::mysql_fetch_all_rows($sql);
 		return $row;  
    }
    
    public function mdp_get_action_seokey($seo_key){
        $sql = " 
                    SELECT
                    ". prefix ."actions.ActName,
                    ". prefix ."actions.ID
                    FROM
                    ". prefix ."categories_lang
                    INNER JOIN ". prefix ."categories ON ". prefix ."categories_lang.CatID = ". prefix ."categories.ID
                    INNER JOIN ". prefix ."com_act ON ". prefix ."com_act.ComID = ". prefix ."categories.ID
                    INNER JOIN ". prefix ."actions ON ". prefix ."com_act.ActID = ". prefix ."actions.ID
                    WHERE
                    ". prefix ."categories_lang.SeoKey = '$seo_key' AND
                    ". prefix ."categories_lang.`Status` = 'show' AND
                    ". prefix ."com_act.`Status` = 'true' AND
                    ". prefix ."actions.`Status` = 'true'";
        $row = parent::mysql_fetch_all_rows($sql);
 		return $row;  
    }
    
    public function mdp_tool_func($model,$active,$str_id)
    {
        $done = false;
        switch($active)
        {
            case 'show':
            case 'hide':
                $status = array('show'=>'true', 'hide'=>'false');
                $done = $model->model_tool_set_status(prefix."permission",$status[$active]," ID IN(".trim($str_id).")");
                
            break;

            case 'delete':
                $done = $model->model_tool_delete(prefix."permission"," ID IN(".trim($str_id).")" );
            break;
        }

        return $done;
    }
    
    public function mdp_get_seokey_from_app($app)
    {
        $app_sql = '';
        if($app != 'all'){
            $app_sql = prefix ."categories.App = '$app' AND";
        }
        
        $sql = "
        
                SELECT
                ". prefix ."categories_lang.SeoKey,
                ". prefix ."categories_lang.CatID,
                ". prefix ."categories.Define
                FROM
                ". prefix ."categories_lang INNER JOIN 
				". prefix ."categories ON 
				". prefix ."categories.ID = ". prefix ."categories_lang.CatID
                WHERE 
                ". prefix ."categories_lang.`Status` = 'show' AND
                ".$app_sql."
                ". prefix ."categories.Define <> 'app' AND
                ". prefix ."categories_lang.LangID = ".LANG."
                ORDER BY
                ". prefix ."categories.Define ASC,
                ". prefix ."categories.ID ASC

        ";
        $row = parent::mysql_fetch_all_rows($sql);
 		return $row;  
    }
    
    public function mdp_get_seokey_catid($cat_id)
    {
        $sql = "
                SELECT 
                ". prefix ."categories_lang.SeoKey
                FROM
                ". prefix ."categories
                INNER JOIN ". prefix ."categories_lang ON ". prefix ."categories.ID = ". prefix ."categories_lang.CatID
                WHERE
                ". prefix ."categories_lang.LangID = ".LANG." AND
                ". prefix ."categories.ID = $cat_id AND
                ". prefix ."categories_lang.`Status` = 'show'
        ";
		$rs = parent::mysql_fetch_one_rows($sql);
		parent::mysql_close_connection();		
        if(empty($rs)){
            return false;
        }else{
            return $rs;
        }
    }
    
    public function mdp_get_seokey_action()
    {
        $sql = "
                SELECT
				". prefix ."actions_lang.Name,
                ". prefix ."actions_lang.SeoKey,
                ". prefix ."actions.ID,
                ". prefix ."actions.ActName
                FROM
                ". prefix ."actions
                INNER JOIN ". prefix ."actions_lang ON ". prefix ."actions.ID = ". prefix ."actions_lang.ActID
                WHERE
                ". prefix ."actions_lang.LangID = 
        ".LANG;
		$rs = parent::mysql_fetch_all_rows($sql);
		parent::mysql_close_connection();		
        if(empty($rs)){
            return false;
        }else{
            return $rs;
        }
    } 
    
    public function mdp_get_permission_from_cid($cid)
    {
        $app_sql = '';
        if($cid != 'all'){
            $app_sql = "WHERE ". prefix ."permission.CID = $cid";
        }
        $sql = "
                SELECT *
                FROM
                ". prefix ."permission  
            ".$app_sql;
		$rs = parent::mysql_fetch_all_rows($sql);
		parent::mysql_close_connection();		
        if(empty($rs)){
            return false;
        }else{
            return $rs;
        }
    } 
}

